package com.zwz.controller;

import com.zwz.crawler.mangeInfo.TungeeLicenseQuery;
import com.zwz.entity.QualificationCertificates;
import com.zwz.resp.LicenseInfo;
import com.zwz.service.QualificationService;
import com.zwz.vo.LicVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/qualifications")
public class QualificationController {

    @Autowired
    private QualificationService service;

    @PostMapping
//    public ResponseEntity<?> create(@RequestBody QualificationCertificates cert) {

    public ResponseEntity<?> create( String enterpriseId) {

        //1.获取的对应的行政许可
        List<LicVo> lic = TungeeLicenseQuery.LicenseInfo(enterpriseId);


        //2.获取对应的税务登记

        QualificationCertificates cert=new QualificationCertificates();

        //构建一个新的目的
        for( int i=0;i<lic.size();i++){
            cert.setLatestPublishDate(lic.get(i).getStart());
            cert.setLicenseContent(lic.get(i).getContent());
            cert.setLicenseStartDate(lic.get(i).getStart());
            cert.setLicenseEndDate(lic.get(i).getEnd());
            cert.setLicenseAuthority(lic.get(i).getGov());
            cert.setLicenseContent(lic.get(i).getContent());
            cert.setLicenseFileName(lic.get(i).getName());
            service.save( cert);
        }

        try {

            return ResponseEntity.status(HttpStatus.CREATED).body(cert);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/valid-licenses")
    public ResponseEntity<List<QualificationCertificates>> getValidLicenses() {
        return ResponseEntity.ok(service.getValidLicenses());
    }

//    @GetMapping("/tax-type/{type}")
//    public ResponseEntity<List<QualificationCertificates>> getByTaxType(
//            @PathVariable String type) {
//        return ResponseEntity.ok(service.getByTaxType(type));
//    }
}